using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Drawing;
using System.Collections;

namespace Pix2Gallery2
{
    public class Gallery2Url
    {
        public string m_GalleryTitle;
        public string m_GalleryUrl;
        public string m_GalleryUserName;
        public string m_GalleryPassword;
    }
    //20080924 wcf Generalize the PSE to Gallery2 field mappings
    public class PseField2Gallery2Field
    {
        public string m_id;
        public string m_identifier;
        public string m_data_type;
        public string m_multi_item;
        public string m_gallery2field;
    }
    public class Pix2Gallery2Settings
    {
        private bool appSettingsChanged;

        public string lastErrorMessage;

        private string m_defaultGalleryUrl;

        private List<Gallery2Url> m_Gallery2Urls;

        private bool uselongTitles;
        private bool uselongDescriptions;
        private bool uselongSummaries;

        private List<PseField2Gallery2Field> m_PseField2Gallery2Fields;
        // >> 20070911 SM
        //   private string m_PSE_TitlePopulatedBy;
        //   private string m_PSE_SummaryPopulatedBy;
        //   private string m_PSE_DescriptionPopulatedBy;
        private string m_PSE_DefaultUploadPictureSize;
        private bool m_PSE_DefaultConvertToJPG;
        // << 20070911 SM
        private string m_defaultPSEdb;
        private bool m_PSE_HideInstructionsOnStartup; //20080326 SM

        //20080924 wcf
        public List<PseField2Gallery2Field> PseField2Gallery2Fields
        {
            get
            {
                return m_PseField2Gallery2Fields;
            }
            set
            {
                m_PseField2Gallery2Fields = value;
                appSettingsChanged = true;
            }
        }
        public bool AppSettingsChanged
        {
            get
            {
                return appSettingsChanged;
            }
            set
            {
                appSettingsChanged = value;
            }
        }

        public string DefaultGalleryUrl
        {
            get
            {
                return m_defaultGalleryUrl;
            }
            set
            {
                if (value != m_defaultGalleryUrl)
                {
                    m_defaultGalleryUrl = value;
                    appSettingsChanged = true;
                }
            }
        }
        //20080926 wcf lets persist the default Elements db since for most people this won't change after the initial use
        public string DefaultPSEdb
        {
            get
            {
                return m_defaultPSEdb;
            }
            set
            {
                if (value != m_defaultPSEdb)
                {
                    m_defaultPSEdb = value;
                    appSettingsChanged = true;
                }
            }
        }
        public bool UseLongTitles
        {
            get
            {
                return uselongTitles;
            }
            set
            {
                uselongTitles = value;
            }
        }
        public bool UseLongDescriptions
        {
            get
            {
                return uselongDescriptions;
            }
            set
            {
                uselongDescriptions = value;
            }
        }
        public bool UseLongSummaries
        {
            get
            {
                return uselongSummaries;
            }
            set
            {
                uselongSummaries = value;
            }
        }

        //>> 20070911 SM
        //public string PSE_TitlePopulatedBy
        //{
        //    get
        //    {
        //        return m_PSE_TitlePopulatedBy;
        //    }
        //    set
        //    {
        //        if (value != m_PSE_TitlePopulatedBy)
        //        {
        //            m_PSE_TitlePopulatedBy = value;
        //            appSettingsChanged = true;
        //        }
        //    }
        //}
        //public string PSE_SummaryPopulatedBy
        //{
        //    get
        //    {
        //        return m_PSE_SummaryPopulatedBy;
        //    }
        //    set
        //    {
        //        if (value != m_PSE_SummaryPopulatedBy)
        //        {
        //            m_PSE_SummaryPopulatedBy = value;
        //            appSettingsChanged = true;
        //        }
        //    }
        //}
        //public string PSE_DescriptionPopulatedBy
        //{
        //    get
        //    {
        //        return m_PSE_DescriptionPopulatedBy;
        //    }
        //    set
        //    {
        //        if (value != m_PSE_DescriptionPopulatedBy)
        //        {
        //            m_PSE_DescriptionPopulatedBy = value;
        //            appSettingsChanged = true;
        //        }
        //    }
        //}
        public string PSE_DefaultUploadPictureSize
        {
            get
            {
                return m_PSE_DefaultUploadPictureSize;
            }
            set
            {
                if (value != m_PSE_DefaultUploadPictureSize)
                {
                    m_PSE_DefaultUploadPictureSize = value;
                    appSettingsChanged = true;
                }
            }
        }
        public bool PSE_DefaultConvertToJPG
        {
            get
            {
                return m_PSE_DefaultConvertToJPG;
            }
            set
            {
                if (value != m_PSE_DefaultConvertToJPG)
                {
                    m_PSE_DefaultConvertToJPG = value;
                    appSettingsChanged = true;
                }
            }
        }
        //<< 20070911 SM
        //>> 20080326 SM
        public bool PSE_HideInstructionsOnStartup
        {
            get
            {
                return m_PSE_HideInstructionsOnStartup;
            }
            set
            {
                if (value != m_PSE_HideInstructionsOnStartup)
                {
                    m_PSE_HideInstructionsOnStartup = value;
                    AppSettingsChanged = true;
                }
            }
        }
        //<< 20080326 SM
        public List<Gallery2Url> Gallery2Urls
        {
            get
            {
                return m_Gallery2Urls;
            }
            set
            {
                m_Gallery2Urls = value;
                appSettingsChanged = true;
            }
        }

        // Serializes the class to the config file
        // if any of the settings have changed.
        public bool SaveAppSettings()
        {
            if (this.appSettingsChanged)
            {
                lastErrorMessage = "";

                StreamWriter myWriter = null;
                XmlSerializer mySerializer = null;
                try
                {
                    // Create an XmlSerializer for the Pix2Gallery2Settings type.
                    mySerializer = new XmlSerializer(typeof(Pix2Gallery2Settings));
                    myWriter = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Pix2Gallery2.config", false);
                    // Serialize this instance of the Pix2Gallery2Settings class to the config file.
                    mySerializer.Serialize(myWriter, this);
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(ex.Message);
                    lastErrorMessage = ex.Message;
                }
                finally
                {
                    // If the FileStream is open, close it.
                    if (myWriter != null)
                    {
                        myWriter.Close();
                    }
                }
            }
            return appSettingsChanged;
        }

        // Deserializes the class from the config file.
        public bool LoadAppSettings()
        {
            lastErrorMessage = "";

            XmlSerializer mySerializer = null;
            FileStream myFileStream = null;
            bool fileExists = false;

            try
            {
                // Create an XmlSerializer for the Pix2Gallery2Settings type.
                mySerializer = new XmlSerializer(typeof(Pix2Gallery2Settings));
                //FileInfo fi = new FileInfo(@"Pix2Gallery2.config");
                FileInfo fi = new FileInfo(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Pix2Gallery2.config");
                // If the config file exists, open it.
                if (fi.Exists)
                {
                    myFileStream = fi.OpenRead();
                    // Create a new instance of the ApplicationSettings by deserializing the config file.
                    Pix2Gallery2Settings myAppSettings = (Pix2Gallery2Settings)mySerializer.Deserialize(myFileStream);
                    // Assign the property values to this instance of the Pix2Gallery2Settings class.
                    this.m_defaultGalleryUrl = myAppSettings.m_defaultGalleryUrl;
                    this.m_Gallery2Urls = myAppSettings.m_Gallery2Urls;
                    //20080925 wcf load the options list and the default PSE DB path
                    this.m_PseField2Gallery2Fields = myAppSettings.m_PseField2Gallery2Fields;
                    this.m_defaultPSEdb = myAppSettings.m_defaultPSEdb;
                    this.uselongTitles = myAppSettings.uselongTitles;
                    this.uselongDescriptions = myAppSettings.uselongDescriptions;
                    this.uselongSummaries = myAppSettings.uselongSummaries;
                    //>> 20070911 SM
                    //this.m_PSE_DescriptionPopulatedBy = myAppSettings.m_PSE_DescriptionPopulatedBy;
                    //this.m_PSE_SummaryPopulatedBy = myAppSettings.m_PSE_SummaryPopulatedBy;
                    //this.m_PSE_TitlePopulatedBy = myAppSettings.m_PSE_TitlePopulatedBy;
                    this.m_PSE_DefaultUploadPictureSize = myAppSettings.m_PSE_DefaultUploadPictureSize;
                    this.m_PSE_DefaultConvertToJPG = myAppSettings.m_PSE_DefaultConvertToJPG;
                    //<< 20070911 SM
                    this.m_PSE_HideInstructionsOnStartup = myAppSettings.m_PSE_HideInstructionsOnStartup; //20080326 SM
                    fileExists = true;
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
                lastErrorMessage = ex.Message;
            }
            finally
            {
                // If the FileStream is open, close it.
                if (myFileStream != null)
                {
                    myFileStream.Close();
                }
            }

            return fileExists;
        }
    }
}
